package com.skivingcloud.admin.sys.controller;


import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.skivingcloud.admin.annotation.ModifyAnnotaionFactory;
import com.skivingcloud.admin.annotation.UserOperateLog;
import com.skivingcloud.admin.sys.entity.Tenant;
import com.skivingcloud.admin.sys.entity.TenantMenu;
import com.skivingcloud.admin.sys.entity.User;
import com.skivingcloud.admin.sys.service.ISysMenuService;
import com.skivingcloud.admin.sys.service.ITenantService;
import com.skivingcloud.admin.utils.UserUtil;
import com.skivingcloud.common.exception.BusinessException;
import com.skivingcloud.common.utils.PageUtils;
import com.skivingcloud.common.utils.R;
import com.skivingcloud.common.utils.TreeUtils;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 * 租户表 前端控制器
 * </p>
 *
 * @author skivingcloud
 * @since 2022-01-28
 */
@RestController
@RequestMapping("/sys/tenant")
public class TenantController {

	private static Logger log = LoggerFactory.getLogger(TenantController.class);
	
	@Autowired
	private ITenantService tenantService;
	@Autowired
	private ISysMenuService sysMenuService;
	/**
	 * 分页查询
	 */
	@PostMapping("/list")
	public R list(@RequestBody Map<String, Object> params) {
		try {
			/*Map<String, Object> params = new HashMap<String, Object>();
			params.put("page", page);
			params.put("limit", limit);
			params.put("dataForm", dataForm);*/
			PageUtils pages = tenantService.queryPage(params);
			return R.ok().put("page", pages);
		} catch (Exception e) {
			log.error("查询数据失败", e);
			return R.error("查询数据失败");
		}
	}

	/**
	 * 根据id查询信息
	 * 
	 * @param id
	 * @return
	 */
	@GetMapping("/info/{id}")
	public R info(@PathVariable("id") String id) {
		try {
			Tenant tenant = tenantService.getById(id);
			return R.ok().put("tenant", tenant);
		} catch (Exception e) {
			log.error("查询数据失败", e);
			return R.error("查询数据失败");
		}
	}

	/**
	 * 新增租户
	 * 
	 * @param tenant
	 * @return
	 */
	@UserOperateLog(module = "系统功能", methods = "save", description = "新增租户信息", functionNo = "101056")
	@PostMapping("/save")
	public R save(@RequestBody Tenant tenant) {
		try {
			User user = UserUtil.getUser();
			tenantService.save(tenant, user);
			// 给日志注解传入业务id
			ModifyAnnotaionFactory.newInstance().ModifyAnnotation(getClass(), UserOperateLog.class, "save", "bussid",
					tenant.toString(), tenant.getClass());
			return R.ok();
		} catch (BusinessException e) {
			return R.error(e.getMessage());
		} catch (Exception e) {
			log.error("保存租户信息失败", e);
			return R.error("保存租户信息失败");
		}
	}

	/**
	 * 更新租户信息
	 * 
	 * @param tenant
	 * @return
	 */
	@UserOperateLog(module = "系统功能", methods = "update", description = "更新租户信息", functionNo = "101057")
	@PostMapping("/update")
	public R update(@RequestBody Tenant tenant) {
		try {
			User user = UserUtil.getUser();
			tenantService.update(tenant, user);
			// 给日志注解传入业务id
			ModifyAnnotaionFactory.newInstance().ModifyAnnotation(getClass(), UserOperateLog.class, "update", "bussid",
					tenant.toString(), tenant.getClass());
			return R.ok();
		} catch (BusinessException e) {
			return R.error(e.getMessage());
		} catch (Exception e) {
			log.error("保存租户信息失败", e);
			return R.error("保存租户信息失败");
		}
	}

	/**
	 * 删除租户信息
	 * 
	 * @param roleIds
	 * @return
	 */
	@PostMapping("/delete")
	// @RequiresPermissions("sys:role:delete")
	@UserOperateLog(module = "系统功能", methods = "delete", description = "删除租户信息", functionNo = "101058")
	public R delete(@RequestBody String[] roleIds) {
		try {
			for (String roleId : roleIds) {
				if ("1".equals(roleId)) {
					return R.error("内置租户请勿删除！");
				}
			}
			tenantService.deleteBatch(roleIds);
			ModifyAnnotaionFactory.newInstance().ModifyAnnotation(getClass(), UserOperateLog.class, "delete", "bussid",
					roleIds.toString(), roleIds.getClass());
			return R.ok();
		} catch (Exception e) {
			log.error("删除租户信息失败", e);
			return R.error("删除租户信息失败");
		}
	}
	
	/**
	 * 查询租户信息{key:'', value:''}
	 * 
	 * @return
	 */
	@GetMapping("/getTenantSelect")
	public R getTenantSelect() {
		List<Map<String, Object>> list = tenantService.getTenantSelect();
		return R.ok().put("list", list);
	}

	/**
	 * 根据租户id加载资源树权限
	 * 
	 * @param id 角色id
	 * @return
	 * @throws Exception
	 */
	@GetMapping("/getSysTenantResTree/{id}")
	public R getSysTenantResTree(@PathVariable("id") String id) throws Exception {
		List<String> terminals = new ArrayList<String>();
		User user = UserUtil.getUser();
		List<TreeUtils> list = sysMenuService.gerResTreeByTerminal(terminals, user);
		List<TenantMenu> asingRes = new ArrayList<TenantMenu>();
		asingRes = tenantService.getResTreeByTenantId(id);
		return R.ok().put("tree", list).put("asingRes", asingRes);
	}

	/**
	 * 设置租户资源范围
	 * 
	 * @param tenant
	 * @return
	 */
	@PostMapping("/setSysTenantMenu")
	@UserOperateLog(module = "系统功能", methods = "setRoleMenu", description = "设置租户资源权限范围", functionNo = "101060")
	public R setSysTenantMenu(@RequestBody Tenant tenant) {
		try {
			User user = UserUtil.getUser();
			tenantService.setSysTenantMenu(tenant, user);
			ModifyAnnotaionFactory.newInstance().ModifyAnnotation(getClass(), UserOperateLog.class, "setSysTenantMenu",
					"bussid",
					tenant.getId().toString(), tenant.getClass());
			return R.ok();
		} catch (Exception e) {
			log.error("设置资源权限失败", e);
			return R.error("设置资源权限失败");
		}
	}
}
